﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace SPAzureBlobField.Classes
{

    public class SPAzureBlobPropertiesControl : UserControl, IFieldEditor
    {
        protected TextBox txtAzureAccountName;
        protected TextBox txtAzureAccountKey;
        protected TextBox txtAzureAccountBlobContainer;

        // Called automatically when control is instanciated.
        public void InitializeWithField(SPField field)
        {
            if (!this.IsPostBack && (field != null))
            {
                SPAzureBlobField myAzureField = field as SPAzureBlobField;
                txtAzureAccountName.Text = myAzureField.AzureAccountName;
                txtAzureAccountKey.Text = myAzureField.AzureAccountKey;
                txtAzureAccountBlobContainer.Text = myAzureField.AzureAccountBlobContainer;
            }
        }

        // Called once values are saved.
        public void OnSaveChange(SPField field, bool isNew)
        {
            SPAzureBlobField myAzureField = field as SPAzureBlobField;

            if (isNew)
            {
                myAzureField.UpdateCustomProperty("AzureAccountName", txtAzureAccountName.Text);
                myAzureField.UpdateCustomProperty("AzureAccountKey", txtAzureAccountKey.Text);
                myAzureField.UpdateCustomProperty("AzureAccountBlobContainer", txtAzureAccountBlobContainer.Text);
            }
            else
            {
                myAzureField.AzureAccountName = txtAzureAccountName.Text;
                myAzureField.AzureAccountKey = txtAzureAccountKey.Text;
                myAzureField.AzureAccountBlobContainer = txtAzureAccountBlobContainer.Text;
            }
        }

        // Used to define is the control has to appear as a separate section.
        public bool DisplayAsNewSection
        {
            get
            {
                return false;
            }
        }
    }
}
